package com.health.trans.processor.writer;

import com.health.trans.annotation.Writer;
import com.health.trans.kit.WriterConfig;
import com.health.trans.pojo.JobConfig;
import com.health.trans.pojo.SqlSentence;
import com.health.trans.util.DataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;
import java.util.List;

/**
 * @author nqlz
 */
@Slf4j
@Writer
public class JdbcWriter implements CsWriter {

    @Override
    public <T> ItemWriter<T> getWriter(WriterConfig<T> writerConfig) {
        JdbcBatchItemWriter<T> tMyWriter = new JdbcBatchItemWriter<T>() {
        };
        tMyWriter.setSql(writerConfig.getSql());
        tMyWriter.setJdbcTemplate(new NamedParameterJdbcTemplate(writerConfig.getDataSource()));
        tMyWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
        tMyWriter.setDataSource(writerConfig.getDataSource());
        return tMyWriter;
    }
}
